import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.min.css'
import store from './store'

import {postRequest} from "@/utils/api";
import {getRequest} from "@/utils/api";
import {putRequest} from "@/utils/api";
import {deleteRequest} from "@/utils/api";
import {initMenu} from "@/utils/menus";

Vue.prototype.postRequest=postRequest;
Vue.prototype.getRequest=getRequest;
Vue.prototype.putRequest=putRequest;
Vue.prototype.deleteRequest=deleteRequest;


//注册 前置导航守卫
router.beforeEach((to, from, next) => {

  if(window.sessionStorage.getItem('tokenStr')){
    //初始化菜单数据
    initMenu(router,store);
    if(!window.sessionStorage.getItem('user')){
       return getRequest('/admin/info').then(resp=>{
        //判断 是否获取到用户信息数据
        //console.log(resp);
        if(resp){
          //获取到 存入sessionStorage
          window.sessionStorage.setItem('user',JSON.stringify(resp))
          next();
        }
      })
    }
    next();
  }else if(to.path=='/'){
    next();
  }else{
    //回到登录页面
    next('/?redirect=' + to.path); // aaa
  }

})

/*
router.beforeEach((to, from, next) => {
  if(window.sessionStorage.getItem('tokenStr')){
    initMenu(router,store);
    if(!window.sessionStorage.getItem('user')){
      return getRequest('/admin/info').then(resp=>{
        if(resp){
          window.sessionStorage.setItem("user",JSON.stringify(resp));
          next();
        }
      })
    }
    next();
  }else {
    if(to.path=='/'){
      next();
    }
  }
  next();
})
*/

Vue.use(ElementUI);
Vue.config.productionTip = false
// el:'#app'
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
